namespace ThingsGateway.Application.Core;
/// <summary>
/// 上传设备运行态API服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.RunTimeGroupName, Order = 300)]
public class UploadDeviceRunTimeService : IDynamicApiController, ITransient
{
    private readonly UploadService _uploadService;
    private readonly SqlSugarRepository<UploadDevice> _deviceRep;
    /// <summary>
    /// 网关数据都做了数据权限，所以RunTime数据也需要判断数据权限，这里直接使用数据库返回的标识再筛选
    /// </summary>
    public UploadDeviceRunTimeService(
        SqlSugarRepository<UploadDevice> deviceRep,
        RootServices rootServices
        )
    {
        _uploadService = rootServices.UploadService;
        _deviceRep = deviceRep;
    }

    /// <summary>
    /// 获取设备状态分页列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet("/uploadstatus/device/page")]
    public async Task<SqlSugarPagedList<UploadDevice>> GetDevicePage([FromQuery] PageUploadDeviceInput input)
    {
        //    //先获取数据库中存在的设备
        //    var data = await _deviceRep.AsQueryable()
        //.WhereIF(!string.IsNullOrWhiteSpace(input.Name?.Trim()), u => u.Name.Contains(input.Name))
        //.WhereIF(!string.IsNullOrWhiteSpace(input.PluginName?.Trim()), u => u.DriverAssembleName.Contains(input.PluginName))
        //.OrderBy(u => u.CreateTime).ToPagedListAsync(input.Page, input.PageSize);

        var runTimeData = await _uploadService.UploadCores
            //.Where(it => data.Items.Any(a => a.Id == it.DeviceId))
            .Select(it => it.DeviceCopy)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name?.Trim()), u => u.Name.Contains(input.Name))
            .WhereIF(!string.IsNullOrWhiteSpace(input.PluginName?.Trim()), u => u.DriverAssembleName.Contains(input.PluginName))
            .OrderBy(u => u.CreateTime)
            .ToPagedListAsync(input.Page, input.PageSize);
        return runTimeData;
    }


}